home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / ctutor.zip / CHAP4.TXT < prev    next >
Text File  |  1987-07-04  |  33KB  |  718 lines

  1.                  Chapter 4 - Assignment & Logical compares
  2.  
  3.  
  4.                        INTEGER ASSIGNMENT STATEMENTS
  5.  
  6.              Load the file INTASIGN.C and display it for an  example
  7.         of  assignment statements.   Three variables are defined for
  8.         use  in the program and the rest of the program is merely  a
  9.         series of illustrations of various assignments.   The  first
  10.         two  lines  of  the assignment statements  assign  numerical
  11.         values  to "a" and "b",  and the next four lines  illustrate
  12.         the  five  basic arithmetic functions and how to  use  them.
  13.         The  fifth is the modulo operator and gives the remainder if
  14.         the two variables were divided.   It can only be applied  to
  15.         "int"  or  "char"  type  variables,   and  of  course  "int"
  16.         extensions such as "long",  "short",  etc.  Following these,
  17.         there  are two lines illustrating how to combine some of the
  18.         variables  in  some complex math expressions.   All  of  the
  19.         above examples should require no comment except to say  that
  20.         none  of  the equations are meant to be particularly  useful
  21.         except as illustrations.
  22.  
  23.              The  next  two expressions are perfectly acceptable  as
  24.         given,  but we will see later in this chapter that there  is
  25.         another way to write these for more compact code.
  26.  
  27.              This leaves us with the last two lines which may appear
  28.         to  you  as being very strange.   The C compiler  scans  the
  29.         assignment  statement from right to left,  (which may seem a
  30.         bit odd since we do not read that way),  resulting in a very
  31.         useful construct,  namely the one given here.   The compiler
  32.         finds the value 20, assigns it to "c", then continues to the
  33.         left finding that the latest result of a calculation  should
  34.         be  assigned to "b".   Thinking that the latest  calculation
  35.         resulted in a 20,  it assigns it to "b" also,  and continues
  36.         the leftward scan assigning the value 20 to "a" also.   This
  37.         is a very useful construct when you are initializing a group
  38.         of  variables.   The last statement illustrates that  it  is
  39.         possible  to actually do some calculations to arrive at  the
  40.         value  which  will be assigned to all three  variables.   In
  41.         fact,  the rightmost expression can contain variables,  even
  42.         "a", "b", & "c".
  43.  
  44.              The  program has no output, so compiling and  executing
  45.         this  program  will be very uninteresting.  Since  you  have
  46.         already  learned how to display some integer  results  using
  47.         the "printf" function, it would be to your advantage to  add
  48.         some output statements to this program to see if the various
  49.         statements do what you think they should do.
  50.  
  51.              This would be a good time for a preliminary  definition
  52.         of  a  rule to be followed in C.   The data definitions  are
  53.         always given before any executable statements in any program
  54.         block.   This is why the variables are defined first in this
  55.  
  56.  
  57.                                   Page 20
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.                  Chapter 4 - Assignment & Logical compares
  68.  
  69.  
  70.         program and in every C program.  If you try to define a  new
  71.         variable  after  executing  some  statements,  the  Turbo  C
  72.         compiler, and every other C compiler will issue an error.
  73.  
  74.                            ADDITIONAL DATA TYPES
  75.  
  76.              Loading and editing MORTYPES.C will illustrate how some
  77.         additional  data  types can be used.   Once  again  we  have
  78.         defined  a  few integer type variables which you  should  be
  79.         fairly  familiar  with  by now,  but we have added  two  new
  80.         types, the "char", and the "float".
  81.  
  82.              The  "char"  type  of data is nearly the  same  as  the
  83.         integer  except that it can only be assigned values  between
  84.         -128 to 127, since it is stored in only one byte of  memory.
  85.         The "char" type of data is usually used for ASCII data, more
  86.         commonly  known  as  text.  The text  you  are  reading  was
  87.         originally written on a computer with a word processor  that
  88.         stored the words in the computer one character per byte.  In
  89.         contrast,  the integer data type is stored in two  bytes  of
  90.         computer memory on microcomputers using Turbo C.
  91.  
  92.                               DATA TYPE MIXING
  93.  
  94.              It  would be profitable at this time to discuss the way
  95.         C handles the two types "char" and "int".  Most functions in
  96.         C  that are designed to operate with integer type  variables
  97.         will work equally well with character type variables because
  98.         they  are a form of an integer variable.   Those  functions,
  99.         when called on to use a "char" type variable,  will actually
  100.         promote  the "char" data into integer data before using  it.
  101.         For this reason, it is possible to mix "char" and "int" type
  102.         variables in nearly any way you desire.   The compiler  will
  103.         not get confused,  but you might.  It is good not to rely on
  104.         this too much, but to carefully use only the proper types of
  105.         data where they should be used.
  106.  
  107.               The second new data type is the "float" type of  data,
  108.         commonly  called floating point data.  This is a  data  type
  109.         which  usually  has a very large range, a  large  number  of
  110.         significant digits, and a large number of computer words are
  111.         required  to store it.  The "float" data type has a  decimal
  112.         point  associated with it and, in Turbo C, has an  allowable
  113.         range of from 3.4E-38 to 3.4E+38, and is composed of about 7
  114.         significant digits.
  115.  
  116.                        HOW TO USE THE NEW DATA TYPES
  117.  
  118.              The  first three lines of the program assign values  to
  119.         all nine of the defined variables so we can manipulate  some
  120.         of the data between the different types.
  121.  
  122.  
  123.                                   Page 21
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.                  Chapter 4 - Assignment & Logical compares
  134.  
  135.  
  136.  
  137.              Since,  as  mentioned above,  a "char" data type is  in
  138.         reality  an "integer" data type,  no special  considerations
  139.         need be taken to promote a "char" to an "int",  and a "char"
  140.         type data field can be assigned to an "int" variable.   When
  141.         going the other way, there is no standard, so you may simply
  142.         get garbage if the value of the integer variable is  outside
  143.         the  range of the "char" type variable.   It will  translate
  144.         correctly if the value is within the range of -128 to 127.
  145.  
  146.              The   third   line   illustrates  the   simplicity   of
  147.         translating an integer into a "float",  simply assign it the
  148.         new  value  and the system will do  the  proper  conversion.
  149.         When  going  the  other  way  however,  there  is  an  added
  150.         complication.   Since  there may be a fractional part of the
  151.         floating  point number,  the system must decide what  to  do
  152.         with it.  By definitions , it will truncate it.
  153.  
  154.              This program produces no output, and we haven't covered
  155.         a way to print out "char" and "float" type variables, so you
  156.         can't  really  get  in  to this program and  play  with  the
  157.         results, but the next program will cover this for you.
  158.  
  159.              Compile  and  run  this program, then go  back  to  the
  160.         Edit/Message  screen (by pressing any key) and  observe  the
  161.         message  window.   Notice  that there  are  several  warning
  162.         messages telling you that 6 variables were assigned a  value
  163.         and  never  used.   These  warning  messages  are  for  your
  164.         benefit,  so  it would pay you to keep a close  eye  on  the
  165.         message window when writing and debugging a program.
  166.  
  167.                           LOTS OF VARIABLE TYPES
  168.  
  169.              Load the file LOTTYPES.C and display it on your screen.
  170.         This file contains every standard simple data type available
  171.         in the programming language C.   There are other types,  but
  172.         they  are  the compound types (ie - arrays  and  structures)
  173.         that we will cover in due time.
  174.  
  175.              Observe  the  file.  First we define  a  simple  "int",
  176.         followed by a "long int" which has a range of -2147483648 to
  177.         2147483647  in Turbo C, and a "short int" which has a  range
  178.         that is identical to that for the "int" variable in Turbo C,
  179.         namely  -32768  to  32767. The "unsigned"  is  next  and  is
  180.         defined as the same size as the "int" but with no sign.  The
  181.         "unsigned" then will cover a range of 0 to 65535 in Turbo C.
  182.         It  should be pointed out that when the "long", "short",  or
  183.         "unsigned" is desired, the "int" is optional and is left out
  184.         by  most experienced programmers.  We have  already  covered
  185.         the "char" and the "float", which leaves only the  "double".
  186.         The "double" covers a greater range than the "float" and has
  187.  
  188.  
  189.                                   Page 22
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.                  Chapter 4 - Assignment & Logical compares
  200.  
  201.  
  202.         more  significant digits for more precise calculations.   It
  203.         also  requires more memory to store a value than the  simple
  204.         "float". The "double" in Turbo C covers a range of  1.7E-308
  205.         to 1.7E+308.
  206.  
  207.              Another  diversion  is in order at  this  point.   Your
  208.         Turbo  C  compiler,  like  most other  C  compilers  has  no
  209.         provisions for floating point math, but only double floating
  210.         point  math.   They  will promote a "float"  to  a  "double"
  211.         before  doing  calculations  and  therefore  only  one  math
  212.         library  will  be  needed.   Of  course,  this  is   totally
  213.         transparent  to  you, so you don't need to worry  about  it.
  214.         Because  of  this, you may think that it would  be  best  to
  215.         simply define every floating point variable as double, since
  216.         they  are promoted before use in any calculations, but  that
  217.         may not be a good idea.  A "float" variable requires 4 bytes
  218.         of storage and a "double" requires 8 bytes of storage, so if
  219.         you have a large volume of floating point data to store, the
  220.         "double" will obviously require much more memory.
  221.  
  222.              After  defining  the data types in  the  program  under
  223.         consideration, a numerical value is assigned to each of  the
  224.         defined  variables  in  order to demonstrate  the  means  of
  225.         outputting each to the monitor.
  226.  
  227.                             THE CONVERSION CHARACTERS
  228.  
  229.              Following   is  a  list  of  some  of  the   conversion
  230.         characters  and  the  way  they are  used  in  the  "printf"
  231.         statement.   A  complete  list  of  all  of  the  conversion
  232.         characters  is  given  on page 171 to 178  of  the  Turbo  C
  233.         Reference Guide.
  234.  
  235.              d    decimal notation
  236.              o    octal notation
  237.              x    hexadecimal notation
  238.              u    unsigned notation
  239.              c    character notation
  240.              s    string notation
  241.              f    floating point notation
  242.  
  243.              Each  of  these  is used following a  percent  sign  to
  244.         indicate  the type of output conversion,  and between  those
  245.         two characters, the following  fields may be added.
  246.  
  247.              -    left justification in its field
  248.              (n)  a number specifying minimum field width
  249.              .    to separate n from m
  250.              (m)  significant fractional digits for a float
  251.              l    to indicate a "long"
  252.  
  253.  
  254.  
  255.                                   Page 23
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.                  Chapter 4 - Assignment & Logical compares
  266.  
  267.  
  268.              These  are all used in the examples which are  included
  269.         in the program presently displayed on your monitor, with the
  270.         exception of the string notation which will be covered later
  271.         in  this tutorial. Note especially the variable field  width
  272.         specification  demonstrated in lines 33 to 36.  This is  not
  273.         part of the original definition of C, but it is included  in
  274.         the  proposed  ANSI standard and will become part of  the  C
  275.         language.   Compile and run this program to see what  effect
  276.         the various fields have on the output.
  277.  
  278.              You  now  have the ability to display any of  the  data
  279.         fields  in  the  previous programs and it would be  to  your
  280.         advantage to go back and see if you can display some of  the
  281.         fields anyway you desire.
  282.  
  283.                               LOGICAL COMPARES
  284.  
  285.              Load  and  view  the file  named  COMPARES.C  for  many
  286.         examples  of compare statements in C.   We begin by defining
  287.         and  initializing  nine variables to use  in  the  following
  288.         compare  statements.   This initialization is new to you and
  289.         can be used to initialize variables while they are defined.
  290.  
  291.              The  first  group of compare statements represents  the
  292.         simplest  kinds  of compares since they simply  compare  two
  293.         variables.    Either  variable  could  be  replaced  with  a
  294.         constant and still be a valid compare,  but two variables is
  295.         the general case.  The first compare checks to see if "x" is
  296.         equal  to  "y"  and it uses the double equal  sign  for  the
  297.         comparison.   A single equal sign could be used here but  it
  298.         would have a different meaning as we will see shortly.   The
  299.         second  comparison checks to see if "x" is greater than "z".
  300.  
  301.              The   third   introduces  the   "NOT"   operator,   the
  302.         exclamation,  which can be used to invert the result of  any
  303.         logical  compare.   The  fourth checks for "b" less than  or
  304.         equal to "c",  and the last checks for "r" not equal to "s".
  305.         As  we  learned in the last chapter,  if the result  of  the
  306.         compare  is true,  the statement following the  "if"  clause
  307.         will  be executed and the results are given in the comments.
  308.         Note  that  "less than" and "greater than or equal  to"  are
  309.         also available, but are not illustrated here.
  310.  
  311.              It  would be well to mention the different format  used
  312.         for the "if" statement in this example program.   A carriage
  313.         return  is  not  required as a statement  separator  and  by
  314.         putting the conditional clause on the same line as the "if",
  315.         it adds to the readability of the overall program.
  316.  
  317.  
  318.  
  319.  
  320.  
  321.                                   Page 24
  322.  
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.                  Chapter 4 - Assignment & Logical compares
  332.  
  333.  
  334.                                MORE COMPARES
  335.  
  336.              The  compares  in  the  second group  are  a  bit  more
  337.         involved.  Starting with the first compare, we find a rather
  338.         strange  looking set of conditions in the  parentheses.   To
  339.         understand  this  we must understand just what a  "true"  or
  340.         "false"  is in the C language.   A "false" is defined  as  a
  341.         value  of zero,  and "true" is defined as a non-zero  value.
  342.         Any  integer  or char type of variable can be used  for  the
  343.         result of a true/false test, or the result can be an implied
  344.         integer or char.
  345.  
  346.              Look  at  the  first  compare of the  second  group  of
  347.         compare statements.   The expression "r != s" will  evaluate
  348.         as  a "true" since "r" was set to 0.0 above,  so the  result
  349.         will be a non-zero value.  In Turbo C, it seems to always be
  350.         a  1,  but you could get in trouble if you wrote  a  program
  351.         that depended on it being 1 in all cases.  Good  programming
  352.         practice  would  be  to  not use  the  resulting  1  in  any
  353.         calculations.   Even  though  the  two  variables  that  are
  354.         compared  are "float" variables, the result will be of  type
  355.         "integer".   There is no explicit variable to which it  will
  356.         be  assigned  so  the result of the compare  is  an  implied
  357.         integer.   Finally the resulting number, probably 1 in  this
  358.         case,  is assigned to the integer variable "x".   If  double
  359.         equal signs were used, the phantom value, namely 1, would be
  360.         compared  to  the value of "x", but since the  single  equal
  361.         sign  is  used, the value 1 is simply assigned  to  "x",  as
  362.         though  the  statement were not  in  parentheses.   Finally,
  363.         since  the result of the assignment in the  parentheses  was
  364.         non-zero, the entire expression is evaluated as "true",  and
  365.         "z" is assigned the value of 1000.  Thus we accomplished two
  366.         things  in  this  statement, we assigned "x"  a  new  value,
  367.         probably  1,  and  we assigned "z" the value  of  1000.   We
  368.         covered a lot in this statement so you may wish to review it
  369.         before  going on.  The important things to remember are  the
  370.         values  that  define "true" and "false", and the  fact  that
  371.         several  things can be assigned in a conditional  statement.
  372.         The  value  assigned to "x" was probably a 1,  but  remember
  373.         that the only requirement is that it is nonzero.
  374.  
  375.              The next example should help clear up some of the above
  376.         in your mind.  In this example, "x" is assigned the value of
  377.         "y",  and since the result is 11, the condition is non-zero,
  378.         which is true,   and the variable "z" is therefore  assigned
  379.         222.
  380.  
  381.              The third example, in the second group, compares "x" to
  382.         zero.   If  the result is true,  meaning that if "x" is  not
  383.         zero,  then "z" is assigned the value of 333,  which it will
  384.         be.   The  last  example in this group illustrates the  same
  385.  
  386.  
  387.                                   Page 25
  388.  
  389.  
  390.  
  391.  
  392.  
  393.  
  394.  
  395.  
  396.  
  397.                  Chapter 4 - Assignment & Logical compares
  398.  
  399.  
  400.         concept,  since the result will be true if "x" is  non-zero.
  401.         The compare to zero is not actually needed and the result of
  402.         the compare is true.   The third and fourth examples of this
  403.         group are therefore identical.
  404.  
  405.                         ADDITIONAL COMPARE CONCEPTS
  406.  
  407.              The   third  group  of  compares  will  introduce  some
  408.         additional  concepts,  namely  the  logical  "AND"  and  the
  409.         logical  "OR".   We  assign  the value of 77  to  the  three
  410.         integer  variables  simply to get started  again  with  some
  411.         defined  values.   The  first  compare of  the  third  group
  412.         contains  the new control "&&",  which is the logical "AND".
  413.         The  entire statement reads,  if "x" equals "y" AND  if  "x"
  414.         equals  77 then the result is "true".   Since this is  true,
  415.         the variable z is set equal to 33.
  416.  
  417.              The  next  compare  in this group introduces  the  "||"
  418.         operator which is the "OR".   The statement reads, if "x" is
  419.         greater  than  "y"  OR if "z" is greater than  12  then  the
  420.         result is true.   Since "z" is greater than 12,  it  doesn't
  421.         matter  if "x" is greater than "y" or not,  because only one
  422.         of  the  two conditions must be true for the  result  to  be
  423.         true.  The result is true, so therefore "z" will be assigned
  424.         the value of 22.
  425.  
  426.                              LOGICAL EVALUATION
  427.  
  428.              When a compound expression is evaluated, the evaluation
  429.         proceeds from left to right and as soon as the result of the
  430.         outcome is assured,  evaluation stops.   Namely, in the case
  431.         of  an "AND" evaluation,  when one of the terms evaluates to
  432.         "false",  evaluation is discontinued because additional true
  433.         terms  cannot make the result ever become  "true".   In  the
  434.         case of an "OR" evaluation,  if any of the terms is found to
  435.         be  "true",  evaluation stops because it will be  impossible
  436.         for additional terms to cause the result to be "false".   In
  437.         the case of additionally nested terms,  the above rules will
  438.         be applied to each of the nested levels.
  439.  
  440.                           PRECEDENCE OF OPERATORS
  441.  
  442.              The  question will come up concerning the precedence of
  443.         operators.   Which  operators are evaluated first and  which
  444.         last?   There are many rules about this topic, but  I  would
  445.         suggest  that  you  don't  worry about  it  at  this  point.
  446.         Instead,  use  lots  of  parentheses  to  group   variables,
  447.         constants,  and  operators  in  a  way  meaningful  to  you.
  448.         Parentheses always have the highest priority and will remove
  449.         any  question of which operations will be done first in  any
  450.         particular statements.
  451.  
  452.  
  453.                                   Page 26
  454.  
  455.  
  456.  
  457.  
  458.  
  459.  
  460.  
  461.  
  462.  
  463.                  Chapter 4 - Assignment & Logical compares
  464.  
  465.  
  466.  
  467.              Going  on to the next example in group three,  we  find
  468.         three  simple variables used in the conditional part of  the
  469.         compare.   Since  all  three  are non-zero,  all  three  are
  470.         "true",  and therefore the "AND" of the three variables  are
  471.         true,  leading  to  the result being "true",  and "z"  being
  472.         assigned  the value of 11.   Note that since the  variables,
  473.         "r", "s", and "t" are "float" type variables, they could not
  474.         be  used this way,  but they could each be compared to  zero
  475.         and the same type of expression could be used.
  476.  
  477.              Continuing on to the fourth example of the third  group
  478.         we  find three assignment statements in the compare part  of
  479.         the "if" statement.  If you understood the above discussion,
  480.         you  should have no difficulty understanding that the  three
  481.         variables are assigned their respective new values,  and the
  482.         result  of  all three are non-zero,  leading to a  resulting
  483.         value of "TRUE".
  484.  
  485.                         THIS IS A TRICK, BE CAREFUL
  486.  
  487.              The last example of the third group contains a bit of a
  488.         trick, but since we have covered it above, it is nothing new
  489.         to you.  Notice that the first part of the compare evaluates
  490.         to  "FALSE".   The  remaining parts of the compare  are  not
  491.         evaluated,  because it is an "AND" and it will definitely be
  492.         resolved as a "FALSE" because the first term is  false.   If
  493.         the program was dependent on the value of "y" being set to 3
  494.         in  the  next  part of the compare,  it  will  fail  because
  495.         evaluation  will  cease following the "FALSE" found  in  the
  496.         first  term.   Likewise,  "z" will not be set to 4,  and the
  497.         variable "r" will not be changed.
  498.  
  499.                           POTENTIAL PROBLEM AREAS
  500.  
  501.              The   last   group   of   compares   illustrate   three
  502.         possibilities for getting into a bit of trouble.   All three
  503.         have  the  common  result that "z" will not get set  to  the
  504.         desired value,  but for different reasons.   In the case  of
  505.         the  first  one,  the compare evaluates as "true",  but  the
  506.         semicolon  following the second parentheses  terminates  the
  507.         "if"  clause,  and the assignment statement involving "z" is
  508.         always executed as the next statement.   The "if"  therefore
  509.         has  no  effect  because of the  misplaced  semicolon.   The
  510.         second  statement is much more straightforward  because  "x"
  511.         will  always  be equal to itself,  therefore the  inequality
  512.         will never be true, and the entire statement will never do a
  513.         thing, but is wasted effort.  The last statement will always
  514.         assign  0  to "x" and the compare will therefore  always  be
  515.         "false",  never  executing the conditional part of the  "if"
  516.         statement.
  517.  
  518.  
  519.                                   Page 27
  520.  
  521.  
  522.  
  523.  
  524.  
  525.  
  526.  
  527.  
  528.  
  529.                  Chapter 4 - Assignment & Logical compares
  530.  
  531.  
  532.  
  533.              The  conditional  statement is extremely important  and
  534.         must be thoroughly understood to write efficient C programs.
  535.         If  any  part of this discussion is unclear  in  your  mind,
  536.         restudy  it  until you are confident that you understand  it
  537.         thoroughly before proceeding onward.
  538.  
  539.              Compile and run this program and once again, return  to
  540.         the  Edit/Message  screen to observe the  warning  messages.
  541.         The Turbo C compiler is warning us that we have a  "possibly
  542.         incorrect assignment in function main" because we have  used
  543.         an  assignment statement within the conditional part of  the
  544.         "if" statements.  Once again, the warnings are for our  aid,
  545.         and  should  be carefully attended to.  In  this  case,  the
  546.         assignments   were  purposely  inserted  for   instructional
  547.         purposes  but assignments are not generally used in  compare
  548.         statements  and  Turbo C is warning you that  you  may  have
  549.         intended to use the double equal sign.
  550.  
  551.                            THE CRYPTIC PART OF C
  552.  
  553.              There are three constructs used in C that make no sense
  554.         at   all  when  first  encountered  because  they  are   not
  555.         intuitive,  but they greatly increase the efficiency of  the
  556.         compiled  code  and  are used extensively by  experienced  C
  557.         programmers.   You  should therefore be exposed to them  and
  558.         learn to use them because they will appear in most,  if  not
  559.         all,  of the programs you see in the publications.  Load and
  560.         examine  the file named CRYPTIC.C for examples of the  three
  561.         new constructs.
  562.  
  563.              In  this  program,   some  variables  are  defined  and
  564.         initialized in the same statements for use below.  The first
  565.         executable statement simply adds 1 to the value of "x",  and
  566.         should come as no surprise to you.   The next two statements
  567.         also  add one to the value of "x",  but it is not  intuitive
  568.         that this is what happens.   It is simply by definition that
  569.         this is true.  Therefore, by definition of the C language, a
  570.         double   plus  sign  either  before  or  after  a   variable
  571.         increments  that variable by 1.   Additionally,  if the plus
  572.         signs are before the variable,  the variable is  incremented
  573.         before  it  is used,  and if the plus signs  are  after  the
  574.         variable,  the variable is used,  then incremented.   In the
  575.         next statement, the value of "y" is assigned to the variable
  576.         "z",  then  "y"  is incremented because the plus  signs  are
  577.         after  the  variable  "y".   In the last  statement  of  the
  578.         incrementing  group of example statements,  the value of "y"
  579.         is  incremented then its value is assigned to  the  variable
  580.         "z".
  581.  
  582.  
  583.  
  584.  
  585.                                   Page 28
  586.  
  587.  
  588.  
  589.  
  590.  
  591.  
  592.  
  593.  
  594.  
  595.                  Chapter 4 - Assignment & Logical compares
  596.  
  597.  
  598.              The  next group of statements illustrate decrementing a
  599.         variable by one.   The definition works exactly the same way
  600.         for decrementing as it does for incrementing.   If the minus
  601.         signs are before the variable,  the variable is decremented,
  602.         then  used,  and if the minus signs are after the  variable,
  603.         the variable is used, then decremented.
  604.  
  605.                       THE CRYPTIC ARITHMETIC OPERATOR
  606.  
  607.              Another  useful but cryptic operator is the  arithmetic
  608.         operator.   This operator is used to modify any variable  by
  609.         some constant value.  The first statement of the "arithmetic
  610.         operator" group of statements simply adds 12 to the value of
  611.         the variable "a".   The second statement does the same,  but
  612.         once again, it is not intuitive that they are the same.  Any
  613.         of the four basic functions of arithmetic, "+", "-", "*", or
  614.         "/",  can  be handled in this way,  by putting the  function
  615.         desired  in  front  of the equal sign  and  eliminating  the
  616.         second  reference to the variable name.   It should be noted
  617.         that  the  expression on the right side  of  the  arithmetic
  618.         operator can be any valid expression,  the examples are kept
  619.         simple for your introduction to this new operator.
  620.  
  621.              Just  like the incrementing and decrementing operators,
  622.         the arithmetic operator is used extensively by experienced C
  623.         programmers and it would pay you well to understand it.
  624.  
  625.                          THE CONDITIONAL EXPRESSION
  626.  
  627.              The  conditional expression is just as cryptic  as  the
  628.         last  two,  but once again it can be very useful so it would
  629.         pay you to understand it.   It consists of three expressions
  630.         within parentheses separated by a question mark and a colon.
  631.         The  expression prior to the question mark is  evaluated  to
  632.         determine  if it is "true" or "false".   If it is true,  the
  633.         expression  between  the  question mark  and  the  colon  is
  634.         evaluated,  and if it is not true,  the expression following
  635.         the  colon  is evaluated.   The result of the evaluation  is
  636.         used for the assignment.   The final result is identical  to
  637.         that  of an "if" statement with an "else" clause.   This  is
  638.         illustrated  by  the  second example  in  this  group.   The
  639.         conditional  expression  has  the added  advantage  of  more
  640.         compact code that will compile to fewer machine instructions
  641.         in the final program.
  642.  
  643.              The  final two lines of this example program are  given
  644.         to  illustrate  a very compact way to assign the greater  of
  645.         two variables "a" or "b" to "c", and to assign the lessor of
  646.         the  same two variables to "c".   Notice how  efficient  the
  647.         code is in these two examples.
  648.  
  649.  
  650.  
  651.                                   Page 29
  652.  
  653.  
  654.  
  655.  
  656.  
  657.  
  658.  
  659.  
  660.  
  661.                  Chapter 4 - Assignment & Logical compares
  662.  
  663.  
  664.              Compile and run this program and notice that once again
  665.         we  get  a few warning to take heed to.  Since we  are  only
  666.         demonstrating how to do these things, they are not  actually
  667.         a problem but in a real program, these assignments would  be
  668.         wasted code.
  669.  
  670.                      TO BE CRYPTIC OR NOT TO BE CRYPTIC
  671.  
  672.              Several students of C have stated that they didn't like
  673.         these  three  cryptic constructs and that they would  simply
  674.         never  use  them.  This will be fine if they never  have  to
  675.         read  anybody  else's  program, or use  any  other  programs
  676.         within their own.  I have found many functions that I wished
  677.         to  use within a program but needed a small modification  to
  678.         use  it, requiring me to understand another  person's  code.
  679.         It  would therefore be to your advantage to learn these  new
  680.         constructs, and use them. They will be used in the remainder
  681.         of this tutorial, so you will be constantly exposed to them.
  682.  
  683.              This has been a long chapter but it contained important
  684.         material  to  get  you  started in using  C.   In  the  next
  685.         chapter,  we  will go on to the building blocks  of  C,  the
  686.         functions.  At that point, you will have enough of the basic
  687.         materials to allow you to begin writing meaningful programs.
  688.  
  689.  
  690.         PROGRAMMING EXERCISES
  691.  
  692.         1.   Write  a program that will count from 1 to 12 and print
  693.              the count, and its square, for each count.
  694.                   1    1
  695.                   2    4
  696.                   3    9   etc.
  697.  
  698.         2.   Write a program that counts from 1 to 12 and prints the
  699.              count  and  its  inversion  to  5  decimal  places  for
  700.              each count. This will require a floating point number.
  701.                   1    1.00000
  702.                   2     .50000
  703.                   3     .33333
  704.                   4     .25000
  705.                   etc.
  706.  
  707.         3.   Write a program that will count from 1 to 100 and print
  708.              only those values between 32 and 39, one to a line.
  709.  
  710.  
  711.  
  712.  
  713.  
  714.  
  715.  
  716.  
  717.                                   Page 30
  718.